var e, t, a = require("../../../@babel/runtime/helpers/interopRequireDefault").default, n = a(require("@vant/weapp/dialog/dialog")), o = a(require("../../../api/api_queryRemain.js")), r = require("../../../api/common.js"), i = a(require("../../../utils/Tips")), u = require("../../../utils/util"), s = a(require("../../../api/wxRequest")), c = a(require("../../../behaviors/auth")), d = a(require("../../../kta/index")), h = getApp().eventBus;

Component({
    behaviors: [ c.default ],
    properties: {},
    data: {
        pageShow: !0,
        promotionFiveShow: new Date().getTime() < new Date("2021-10-29").getTime(),
        apanage: "",
        phone: "",
        amounts: [ {
            value: 30
        }, {
            value: 50
        }, {
            value: 100
        }, {
            value: 300
        } ],
        ADBanners: [],
        resourceBtn: [],
        resourceBanner: [],
        resourceBtnPlaceholder: 0,
        resourceBannerPlaceholder: 0,
        chargeBroadbandStatus: !1,
        swiperConfig: {
            autoplay: !0
        }
    },
    methods: {
        init: function() {
            var e = this;
            this.getAuthUserInfo().then(function(t) {
                var a = t.data.phone;
                e.setData({
                    phone: a
                }), e.getApannage();
            });
        },
        init_chargePageSub: function() {
            var e = this;
            this.chargePageSub = h.on("toChargePage", function(t) {
                var a = t.phone;
                e.setData({
                    phone: a,
                    bindedPhone: a
                }), e.init();
            });
        },
        init_advertisementBanner: function() {
            var e = this;
            (0, r.getBanner)("126608").then(function(t) {
                var a = t.data;
                e.setData({
                    ADBanners: a
                }), console.log("bannerInfo", e.data.ADBanners);
            });
        },
        init_resourceBtn: function() {
            var e = this;
            (0, r.getBanner)("361920").then(function(t) {
                for (var a = t.data, n = [], o = 0; o < 2; o++) {
                    var r = a[o];
                    r && n.push(r);
                }
                e.setData({
                    resourceBtn: n
                }), console.log("resourceBtn", n);
            }).finally(function() {
                e.changeResourceBtnPlaceholderCount();
            });
        },
        init_resourceBanner: function() {
            var e = this;
            (0, r.getBanner)("762785").then(function(t) {
                var a = t.data;
                e.setData({
                    resourceBanner: a
                }), a.length % 2 == 0 ? e.setData({
                    resourceBannerPlaceholder: 0
                }) : e.setData({
                    resourceBannerPlaceholder: 1
                }), console.log("resourceBanner", e.data.resourceBanner);
            });
        },
        init_judgeChargeBroadbandStatus: function() {
            this.judgeChargeBroadbandStatus();
        },
        judgeChargeBroadbandStatus: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, a = t.fromClick, o = void 0 !== a && a, u = this.data.phone, s = u || void 0;
            return o && i.default.loading(), (0, r.discountuser)({
                proId: "1123179121001009248",
                phoneNum: s
            }).then(function(t) {
                o && i.default.loaded();
                var a = t.code, r = t.message, s = t.data;
                return "0" === a && s ? (e.setData({
                    chargeBroadbandStatus: !0
                }), o && wx.navigateTo({
                    url: "/pages/chargeBroadbandActivity/index?phone=".concat(u)
                }), Promise.resolve()) : (console.warn("chargeBroadbandStatus：".concat(r)), e.setData({
                    chargeBroadbandStatus: !1
                }), o && n.default.alert({
                    context: e,
                    message: r
                }), Promise.reject());
            }).catch(function() {
                o && i.default.loaded();
            }).finally(function() {
                e.changeResourceBtnPlaceholderCount();
            });
        },
        afterBindgetphonenumber: function(e) {
            var t = e.data.phone;
            this.setData({
                phone: t
            }), this.getApannage();
        },
        onPhoneInput: function(a) {
            var n = this, o = a.detail.value;
            this.setData({
                phone: o
            }), e || (e = (0, u.debounce)(function() {
                n.getApannage();
            }, 500)), t || (t = (0, u.debounce)(function() {
                n.getChargeBroadbandStatus();
            }, 500)), e(), t();
        },
        handleAmountClick: function(e) {
            var t = e.currentTarget.dataset.value;
            this.chargeAmount(t);
        },
        chargeAmount: function(e) {
            var t = this, a = this.data.phone;
            if ((0, u.testPhone)(a)) {
                i.default.loading();
                var r = 100 * e;
                o.default.charge({
                    phone: a,
                    amountFee: r
                }).then(function(e) {
                    return i.default.loaded(), console.log(e), "0" === e.code ? e.data.pkg ? void s.default.wxPay(e.data).then(function(e) {
                        var t = e.appid, a = e.path;
                        t && "wxc47d9eb33eec8b6a" != t && a ? wx.openEmbeddedMiniProgram({
                            appId: t,
                            path: a,
                            success: function() {
                                console.log("成功打开小程序appid：", t, ";path：", a);
                            },
                            fail: function(e) {
                                console.log(e);
                            }
                        }) : a ? wx.navigateTo({
                            url: a
                        }) : console.log("无可跳转地址");
                    }) : Promise.reject("fail") : ("10002" === e.code ? n.default.confirm({
                        context: t,
                        title: "温馨提示",
                        message: "您未绑定王卡助手，请先绑定",
                        confirmButtonOpenType: "getPhoneNumber",
                        confirmButtonText: "立即绑定"
                    }) : n.default.alert({
                        context: t,
                        message: e.message
                    }), Promise.reject());
                }).catch(function(e) {
                    i.default.loaded(), "fail" === e && n.default.alert({
                        context: t,
                        message: "调起支付失败，请稍候重试"
                    });
                });
            } else i.default.toast("请输入正确的手机号");
        },
        getApannage: function() {
            var e = this, t = this.data.phone;
            t && (0, u.testPhone)(t) ? (o.default.getApanage({
                phone: t
            }).then(function(t) {
                "0" === t.code && e.setData({
                    apanage: t.data
                });
            }), this.judgeChargeBroadbandStatus()) : (this.setData({
                chargeBroadbandStatus: !1
            }), this.changeResourceBtnPlaceholderCount());
        },
        getChargeBroadbandStatus: function() {
            var e = this.data.phone;
            e && (0, u.testPhone)(e) ? this.judgeChargeBroadbandStatus() : (this.setData({
                chargeBroadbandStatus: !1
            }), this.changeResourceBtnPlaceholderCount());
        },
        handleJumpAD: function(e) {
            var t = e.currentTarget.dataset.bannerItem, a = t.miniProgram, n = t.link, o = t.path, r = t.eventId;
            d.default.clickStat(r), a ? (o = 0 === o.indexOf("/") ? o : "/".concat(o), wx.navigateTo({
                url: o
            })) : this.openWebview(n);
        },
        handleJumpChargeBroadbandActivity: function() {
            var e = this.data.phone;
            e ? (0, u.testPhone)(e) ? this.judgeChargeBroadbandStatus({
                fromClick: !0
            }) : i.default.toast("请输入正确的手机号") : i.default.toast("请输入手机号");
        },
        openWebview: function(e) {
            wx.navigateTo({
                url: "/pages/webview/index?url=" + encodeURIComponent(e)
            });
        },
        changeResourceBtnPlaceholderCount: function() {
            var e = this.data, t = e.resourceBtn, a = e.chargeBroadbandStatus ? t.length + 1 : t.length;
            a <= 2 ? this.setData({
                resourceBtnPlaceholder: a % 2
            }) : (a -= 2, this.setData({
                resourceBtnPlaceholder: 3 - a % 3 == 3 ? 0 : 3 - a % 3
            }));
        }
    },
    created: function() {},
    attached: function() {
        this.init(), this.init_chargePageSub(), this.init_advertisementBanner(), this.init_resourceBanner(), 
        this.init_resourceBtn(), this.init_judgeChargeBroadbandStatus();
    },
    ready: function() {},
    moved: function() {},
    detached: function() {
        this.chargePageSub && this.chargePageSub.off();
    }
});